(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			checkParames();
			pageBtns();
		});
	});
	
	function pageBtns(){
		$(".save-btns").click(function(){
			if(checkSave()){
				if(check2()){
					addRule();
				}
			}
					
		});
		
	}
	function checkParames(){
		var parame = ["name","describe","time",
		              "morninginschool1","morningoutschool1","morningabsence1","afternooninschool1","afternoonoutschool1","afternoonabsence1",
		              "morninginschool2","morningoutschool2","morningabsence2","afternooninschool2","afternoonoutschool2","afternoonabsence2",
		              "morninginschool3","morningoutschool3","morningabsence3","afternooninschool3","afternoonoutschool3","afternoonabsence3",
		              "morninginschool4","morningoutschool4","morningabsence4","afternooninschool4","afternoonoutschool4","afternoonabsence4",
		              "morninginschool5","morningoutschool5","morningabsence5","afternooninschool5","afternoonoutschool5","afternoonabsence5",
		              ];
		for(var i = 0; i < parame.length; i++){
			check("rule-"+parame[i],isNull,"null");
		}
	}
	
	function addRule(){
		var trule = getFormData();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var obj = {};
		obj.code=new Date().getTime();
		for(var key in trule){
			obj[key] = trule[key];
		}
		obj.schoolcode = schoolcode;
		var data = {
				cmd:"insertAttendanceRegular",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("添加考勤规则信息成功");
			}else{
				processError("添加失败");
			}
		},null,null);
	}
	
	function getFormData(){
		new setTRule(null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,
				null,null,null,null,null,null,null,null,null,null,null);
		var trule = getTRule();
		var obj = {};

		for(var key in trule){
			if($("#rule-"+key)[0]){
				if($("#rule-"+key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="time"){
						obj[key] = strToDate($("#rule-"+key).val() + ":00")/1000;
					}else if(key=="name"){
						obj[key] = encodeURI($("#rule-"+key).val());
					}else if(key=="describe"){
						obj[key] = encodeURI($("#rule-"+key).val());
					}else{
						obj[key] = $("#rule-"+key).val();
					}
					
				}else if(key=="describe"){
					obj[key] = encodeURI($("#rule-"+key).val());
				}
			}
		}
		var schoolcode=encodeURI($("#user-name",parent.parent.document).attr("schoolcode"));
		obj.schoolcode=schoolcode;
		var userKey = $("#user-name",parent.parent.document).attr("key");
		obj.userkey = userKey;
		var sendcode = $("#user-name",parent.parent.document).attr("code");
		obj.sendcode = sendcode;
		new setTRule(obj.name,obj.time,obj.userkey,obj.schoolcode,obj.describe,
				obj.morninginschool1,obj.morningoutschool1,obj.morningabsence1,obj.afternooninschool1,
				obj.afternoonoutschool1,obj.afternoonabsence1,obj.nightinschool1,obj.nightoutschool1,
				obj.nightabsence1,
				obj.morninginschool2,obj.morningoutschool2,obj.morningabsence2,obj.afternooninschool2,
				obj.afternoonoutschool2,obj.afternoonabsence2,obj.nightinschool2,obj.nightoutschool2,
				obj.nightabsence2,
				obj.morninginschool3,obj.morningoutschool3,obj.morningabsence3,obj.afternooninschool3,
				obj.afternoonoutschool3,obj.afternoonabsence3,obj.nightinschool3,obj.nightoutschool3,
				obj.nightabsence3,
				obj.morninginschool4,obj.morningoutschool4,obj.morningabsence4,obj.afternooninschool4,
				obj.afternoonoutschool4,obj.afternoonabsence4,obj.nightinschool4,obj.nightoutschool4,
				obj.nightabsence4,
				obj.morninginschool5,obj.morningoutschool5,obj.morningabsence5,obj.afternooninschool5,
				obj.afternoonoutschool5,obj.afternoonabsence5,obj.nightinschool5,obj.nightoutschool5,
				obj.nightabsence5,
				obj.morninginschool6,obj.morningoutschool6,obj.morningabsence6,obj.afternooninschool6,
				obj.afternoonoutschool6,obj.afternoonabsence6,obj.nightinschool6,obj.nightoutschool6,
				obj.nightabsence6,
				obj.morninginschool7,obj.morningoutschool7,obj.morningabsence7,obj.afternooninschool7,
				obj.afternoonoutschool7,obj.afternoonabsence7,obj.nightinschool7,obj.nightoutschool7,
				obj.nightabsence7,
				obj.remark,obj.sendcode);
		return getTRule();	
	}
	function check2(){
		var flg = false;
		var checkFails = $(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	function checkSave(){
		var flag = false;
		var parame = ["name","describe","time",
		              "morninginschool1","morningoutschool1","morningabsence1","afternooninschool1","afternoonoutschool1","afternoonabsence1",
		              "morninginschool2","morningoutschool2","morningabsence2","afternooninschool2","afternoonoutschool2","afternoonabsence2",
		              "morninginschool3","morningoutschool3","morningabsence3","afternooninschool3","afternoonoutschool3","afternoonabsence3",
		              "morninginschool4","morningoutschool4","morningabsence4","afternooninschool4","afternoonoutschool4","afternoonabsence4",
		              "morninginschool5","morningoutschool5","morningabsence5","afternooninschool5","afternoonoutschool5","afternoonabsence5",
		              ];
		for(var i = 0; i < parame.length; i++){
			if($("#rule-" + parame[i]).val() == ""){
				$("#rule-" + parame[i]).focus();
				$("#rule-" + parame[i] + "-ex").text("不能为空！");
				return false;
			}else{
				flag = true;
			}
		}
		return flag;
	}
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}

})();